package com.xiaohong.search.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;


import java.util.HashMap;
import java.util.Map;

/**
 * 创建新的索引的Demo
 */
public class ElasticSearchAddDemo {

	private String name;
	private int shards;  //分配的主分片的数量
	private int replicas;  //副本
	private Map<String,Object> mappings;  //
	private String qq;
	private String weixin;
	private String phone;

	public ElasticSearchAddDemo(String name,int shards,int replicas,Map<String,Object> mappings){
		this.name=name;
		this.shards=shards;
		this.replicas=replicas;
		this.mappings=mappings;
	}



	public static void main(String[] args) {

		//创建mappings properties：属性
		Map<String,Object> phone = new HashMap<>();
		phone.put("type","text");
		Map<String,Object> qq = new HashMap<>();
		qq.put("type","text");
		Map<String,Object> weixin = new HashMap<>();
		weixin.put("type","text");
		Map<String,Object> properties = new HashMap<>();
		properties.put("phone",phone);
		properties.put("qq",qq);
		properties.put("weixin",weixin);
		Map<String, Object> mapping = new HashMap<>();
		mapping.put("properties", properties);
		ElasticSearchAddDemo addDemo = new ElasticSearchAddDemo("sse_test",1,0,mapping);
		addDemo.addEsIndex();

	}

	//创建索引的方法
	public void addEsIndex(){

		//当es用用户名和密码连接时
		//初始化ES操作客户端
		/*final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY,
				new UsernamePasswordCredentials("elastic", "123456"));  //es账号密码（默认用户名为elastic）
		RestHighLevelClient client =new RestHighLevelClient(
				RestClient.builder(new HttpHost("192.168.6.1",9200,"http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
					public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
						httpClientBuilder.disableAuthCaching();
						return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
					}
				})
		);*/

		//TODO 当es无密码时可以直接使用下面这个创建客户端
		RestHighLevelClient client =  new RestHighLevelClient(RestClient.builder(new HttpHost(
				//如果是集群再配置多个
				"120.55.57.108",9200,"http"
		)));



		//创建新建索引的请求对象
		//注意索引名要小写
		CreateIndexRequest request = new CreateIndexRequest("sse_test");
		//通过CreateIndexRequest对象设置分片,副本和映射
		// 创建索引时，分配的主分片的数量
		request.settings(Settings.builder().put("index.number_of_shards",1).
				// 创建索引时，为每一个主分片分配的副本分片的数量
				put("index.number_of_replicas",0));
		request.mapping(mappings);
		CreateIndexResponse createIndexResponse;
		//同步方法
		try{
			createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
			boolean acknowledged = createIndexResponse.isAcknowledged();
			boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
			if(acknowledged && shardsAcknowledged){
				System.out.println("索引创建成功!");
			}
			client.close();
		}catch (Exception e){
			e.printStackTrace();
		}

		//异步方法
		/*try{
			client.indices().createAsync(request, RequestOptions.DEFAULT, new ActionListener<CreateIndexResponse>() {
				@Override
				public void onResponse(CreateIndexResponse createIndexResponse) {
					System.out.println("执行情况:" + createIndexResponse);
				}

				@Override
				public void onFailure(Exception e) {
					System.out.println("执行失败的原因:" + e.getMessage()) ;
				}
			});
			client.close();
		}catch (Exception e){
			e.printStackTrace();
		}*/

	}

}
